//
//  HomeViewController.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeProgramaCell.h"
#import "HomeCycleImageCell.h"
#import "EveryDayTouTiaoCell.h"
#import "LifeSeviceHeaderCell.h"
#import "LifeSeviceSubCell.h"
#import "BuyRecommendCell.h"
#import "HomeDataModel.h"
#import "HomeSubModel.h"
#import "HotHouseHeaderCell.h"
//#import "HouseInforCell.h"
#import "RentHouseDetailVC.h"

#import "MyHouseInfoTopCell.h"
#import "HouseParameSub.h"
#import <AVFoundation/AVFoundation.h>
#import "MyMessageListVC.h"
#import "BaseNavigationController.h"
#import "GoodDetailController.h"
#import "LoginViewController.h"
#import "HomeTiShiCell.h"
#import "HouseParameterModel.h"
#import "AboutusController.h"
//#import "HouseParameterModel.h"


#import "ProvinceModel.h"
@interface HomeViewController ()<BuyRecommendCellDelegate,EveryDayTouTiaoCellDelegate>
/** <#note#> */
//@property (nonatomic, strong) NSArray *urls;

/** <#note#> */
@property (nonatomic, strong) HomeDataModel *dataModel;

/** <#note#> */
@property (nonatomic, weak) UILabel *messageLabel;

/** ns */
@property (nonatomic, strong) NSString *cacheTag;



@end

@implementation HomeViewController



- (void)jumpMessageBtnDidClickWith:(HomeSubModel *)model{
    
//    AboutusController *temp = [[AboutusController alloc]init];
//
//    temp.urlString = model.url;
//
//    [self.navigationController pushViewController:temp animated:YES];
    
    
    [self jumpMessage];
    
}




//- (NSArray *)urls{
//    if (!_urls) {
//        _urls = @[@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05530800562251838.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05530800562251838.jpg",@"http://mall.xjscvip.com/data/upload/mobile/special/s0/s0_05531746494804292.jpg"];
//    }
//
//    return _urls;
//}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self calculateTotalGoodsCounts];
    
    
    [self loadData];
  
    
    
    self.cacheTag = @"0";
//    NSLog(@"%@",[myShopCar mj_JSONString]);
    
//    [self performSelector:@selector(openDengWithBtn:) withObject:[UIButton buttonWithType:UIButtonTypeCustom] afterDelay:2.0];
    
//    [self setNav];
    
    myViewModel.rootController = (BaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    

    self.groupTableView.tableFooterView = [[UIView alloc]init];
    
    self.groupTableView.backgroundColor = [UIColor whiteColor];
    
    
    self.groupTableView.rowHeight = UITableViewAutomaticDimension;
    
    self.groupTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.groupTableView.estimatedRowHeight = 100;
    
    [self.groupTableView registerClass:[BaseTableViewCell class] forCellReuseIdentifier:[BaseTableViewCell identifierStr]];
    
     [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeProgramaCell class]) bundle:nil] forCellReuseIdentifier:[HomeProgramaCell identifierStr]];
    
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([EveryDayTouTiaoCell class]) bundle:nil] forCellReuseIdentifier:[EveryDayTouTiaoCell identifierStr]];
    
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeCycleImageCell class]) bundle:nil] forCellReuseIdentifier:[HomeCycleImageCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([LifeSeviceSubCell class]) bundle:nil] forCellReuseIdentifier:[LifeSeviceSubCell identifierStr]];
    
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HotHouseHeaderCell class]) bundle:nil] forCellReuseIdentifier:[HotHouseHeaderCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyHouseInfoTopCell class]) bundle:nil] forCellReuseIdentifier:[MyHouseInfoTopCell identifierStr]];
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeTiShiCell class]) bundle:nil] forCellReuseIdentifier:[HomeTiShiCell identifierStr]];

    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([BuyRecommendCell class]) bundle:nil] forCellReuseIdentifier:[BuyRecommendCell identifierStr]];
    
     [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([LifeSeviceHeaderCell class]) bundle:nil] forCellReuseIdentifier:[LifeSeviceHeaderCell identifierStr]];
    
    
    
    MJWeakSelf
    self.groupTableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        
        [weakSelf lastLoadData];
        
    }];
    
    
    
    
    if (self.dataModel == nil) {
        [weakSelf.groupTableView.mj_header beginRefreshing];
    } else {
        [weakSelf lastLoadData];
    }
    

    
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(lastLoadData) name:@"qianTaiDidReceiveNotification" object:nil];

    
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    
//    //    CGFloat navH = IPhoneX ? 44 : 20;
//    CGFloat tabbarH = IPhoneX ? 83 : 49;
//    
//    if (@available(iOS 11.0, *)) {
//        //        contentInsetAdjustmentBehavior属性也是用来取代automaticallyAdjustsScrollViewInsets属性的，推荐使用这种方式。
//        self.plainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//        
//        self.plainTableView.contentInset = UIEdgeInsetsMake(0, 0, tabbarH, 0);
//        
//    }else{
//        
//        self.plainTableView.contentInset = UIEdgeInsetsMake(0, 0, tabbarH, 0);
//        
//    }
//    
    
}


- (void)setNav{
    
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(login)];
    
    UIView *navRit = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 42, 36)];
    
    navRit.backgroundColor = [UIColor clearColor];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(21, 9, 21, 18)];
    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    
    imageView.image = [UIImage imageNamed:@"homeMessage001"];
    
    UILabel *conutLabel = [[UILabel alloc]initWithFrame:CGRectMake(38, 6, 8, 8)];
    
    conutLabel.font = MYFONT(11);
    
    conutLabel.backgroundColor = [UIColor redColor];
    
    conutLabel.textColor = [UIColor whiteColor];
    
    conutLabel.layer.masksToBounds = YES;
    
    conutLabel.layer.cornerRadius = 4;
    
//    conutLabel.text = @"8";
    
    conutLabel.textAlignment = NSTextAlignmentCenter;
    
    self.messageLabel = conutLabel;
    
    self.messageLabel.hidden = YES;
    
    UIButton *jumpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    jumpBtn.frame = CGRectMake(0, 0, 42, 36);
    
    jumpBtn.backgroundColor = [UIColor clearColor];
    
    [jumpBtn addTarget:self action:@selector(jumpMessage) forControlEvents:UIControlEventTouchUpInside];
    
    [navRit addSubview:imageView];
    
    [navRit addSubview:conutLabel];
    
    [navRit addSubview:jumpBtn];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:navRit];
    
    
}



- (UIStatusBarStyle)preferredStatusBarStyle{
    
    
    return UIStatusBarStyleDefault;
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:YES];


}


- (void)jumpMessage{
    
    if ([self.dataModel.msgStatus isEqualToString:@"1"]) {
        
        
        [self cancelRedDian];
    }
    

    
    MyMessageListVC *mess = [[MyMessageListVC alloc]init];
    
    [self.navigationController pushViewController:mess animated:YES];
    
}

- (void)cancelRedDian{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"uid"] = [UserViewModel shareUserViewModel].user.uid;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Member/updateMsgStatus" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
        
            [weakSelf lastLoadData];
        }
        
    }];
    
    
}

- (void)login{
    
    BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:[[LoginViewController alloc]init]];
    
    [self presentViewController:nav animated:YES completion:nil];
}


- (void)loadData{
    
    if (myViewModel.user.homejsonStr != nil) {
        
        NSLog(@"%@",myViewModel.user.homejsonStr);
        
        NSData *jsonData = [myViewModel.user.homejsonStr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSError *err;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];
        
        if (!err) {
            
            self.dataModel = [HomeDataModel mj_objectWithKeyValues:dic[@"data"]];
            self.dataModel.house = nil;
            myViewModel.rate = self.dataModel.rate;
            myViewModel.kefu = self.dataModel.kefu;
            myViewModel.internet_information = self.dataModel.internet_information;
            
            BaseNavigationController *nav = myViewModel.rootController.childViewControllers[1];
            AboutusController *temp = nav.childViewControllers[0];
            
            temp.urlString = self.dataModel.kefu;
            
            if (!(self.dataModel.advs.count > 0)) {
                
                self.dataModel.advs = nil;
            }
            
            if (!(self.dataModel.goods.count > 0)) {
                self.dataModel.goods = nil;
            }
            
            if (!(self.dataModel.tishi.count > 0)) {
                self.dataModel.tishi = nil;
            }
            
            if (!(self.dataModel.huo.count > 0)) {
                self.dataModel.huo = nil;
            }
            
            if (!(self.dataModel.articles.count > 0)) {
                self.dataModel.articles = nil;
            }
            
            if ([self.dataModel.msgStatus isEqualToString:@"0"]) {
                
                self.messageLabel.hidden = YES;
                
            } else {
                
                self.messageLabel.hidden = NO;
                
            }
            
            
            if (self.dataModel.advs || self.dataModel.huo) {
                
                [self downloadImage];
                
            }
            
//            [self.groupTableView.mj_header endRefreshing];
            
            [self.groupTableView reloadData];
            
       
        }
        
    }
    
}




- (void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    NSString *newMessageId = [[NSUserDefaults standardUserDefaults]objectForKey:@"jpush"];
    
    
    if (newMessageId.length > 0) {
        
         [self cancelRedDian];
        
         [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"jpush"];

        
        if (myViewModel.userIsLogin) {
            
            MyMessageListVC *mess = [[MyMessageListVC alloc]init];
            
            [self.navigationController pushViewController:mess animated:YES];
            
        }
        
        
    }
    

}



- (void)lastLoadData{
    
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //    self.parameterModel.house_tag
    
    if (myViewModel.userIsLogin) {
        
        parameters[@"uid"] = [UserViewModel shareUserViewModel].user.uid;
        
    }
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"home" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//            MYJSONSTR;
            
            myViewModel.user.homejsonStr = [responseObject mj_JSONString];
            weakSelf.dataModel = [HomeDataModel mj_objectWithKeyValues:responseObject[@"data"]];
            
            weakSelf.dataModel.house = nil;
            
            myViewModel.rate = weakSelf.dataModel.rate;
            myViewModel.kefu = weakSelf.dataModel.kefu;
            myViewModel.internet_information = weakSelf.dataModel.internet_information;
            
    
            
            if (myViewModel.hideShopping == YES) {
                
                BaseNavigationController *nav = myViewModel.rootController.childViewControllers[1];
                AboutusController *temp = nav.childViewControllers[0];
                temp.urlString = weakSelf.dataModel.kefu;

            }
            
            
            if (!(weakSelf.dataModel.advs.count > 0)) {
                
                weakSelf.dataModel.advs = nil;
            }
            
            if (!(weakSelf.dataModel.goods.count > 0)) {
                
                weakSelf.dataModel.goods = nil;
            }
            
            if (!(weakSelf.dataModel.tishi.count > 0)) {
                
                weakSelf.dataModel.tishi = nil;
            }
            
            if (!(weakSelf.dataModel.huo.count > 0)) {
                
                weakSelf.dataModel.huo = nil;
            }
            
            if (!(weakSelf.dataModel.articles.count > 0)) {
                
                weakSelf.dataModel.articles = nil;
            }
            
//            if (myViewModel.hideRedDian == NO) {
            
                if ([weakSelf.dataModel.msgStatus isEqualToString:@"0"]) {
                    
//                    myViewModel.hideRedDian = YES;
                    
                    self.messageLabel.hidden = YES;
                    
                } else {
                    
//                    myViewModel.hideRedDian = NO;
                    self.messageLabel.hidden = NO;
                    
                }
                
//            }
            
           
            
            
            if (weakSelf.dataModel.advs || weakSelf.dataModel.huo) {
                
                [weakSelf downloadImage];
                
            }
            
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            [weakSelf.groupTableView reloadData];
            
            [myViewModel.user saveUserInfo];
            
            if ([weakSelf.cacheTag isEqualToString:@"0"]) {
                
                weakSelf.cacheTag  = @"1";
                
                [weakSelf loadAreaData];
                
                [weakSelf loadCommendGoods];
                
                [weakSelf loadCategoryInfo];
                
            }
            
        } else {
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
            //weakSelf.dataModel = [[HomeDataModel alloc]init];
            
            [weakSelf.groupTableView.mj_header endRefreshing];
            
            if ([weakSelf.cacheTag isEqualToString:@"0"]) {
                
                weakSelf.cacheTag  = @"1";
                
                [weakSelf loadAreaData];
                
                [weakSelf loadCommendGoods];
                
                [weakSelf loadCategoryInfo];
                
            }
        }
        
        
    }];
   
}




//- (void)setupValues:(HouseParameterModel *)model{
//
//    model.house_face_name = @"东南东北";
//    model.house_language_name = @"马来西亚";
//    model.house_decorate_name = @"精装修";

//    for (HouseParameSub *sub1 in model.position) {
//        if ([sub1.class_id isEqualToString:model.house_face]) {
//            model.house_face_name = sub1.name;
//        }
//    }
//
//
//
//
//    for (HouseParameSub *sub2 in model.language) {
//        if ([sub2.class_id isEqualToString:model.house_language]) {
//            model.house_language_name = sub2.name;
//        }
//    }
//
//
//    for (HouseParameSub *sub2 in model.decorateStyle) {
//        if ([sub2.class_id isEqualToString:model.house_decorate]) {
//            model.house_decorate_name = sub2.name;
//        }
//    }
//}




- (void)downloadImage{
    
    dispatch_group_t group = dispatch_group_create();
    
    if (self.dataModel.advs) {
        
        dispatch_group_enter(group);
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.dataModel.advs.firstObject.adv_image] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            NSLog(@"111111111");
            
            dispatch_group_leave(group);
            
        }];
    }
    
    
    
    
    if (self.dataModel.huo) {
       
        dispatch_group_enter(group);
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.dataModel.huo.firstObject.adv_image] options:kNilOptions progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
               NSLog(@"22222222222");
            dispatch_group_leave(group);
            
        }];
    }
    
  
    
    
    
    MJWeakSelf;
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    NSLog(@"3333333333");
        
        [weakSelf.groupTableView.mj_header endRefreshing];
        
        [weakSelf.groupTableView reloadData];
        
        
        
    });
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (self.dataModel == nil) {
        
        return 0;
        
    } else {
        
        NSInteger count = 1;
        
        if (self.dataModel.advs) {
            count++;
        }
        
        if (self.dataModel.articles) {
            count++;
        }
        
        
        if (self.dataModel.huo) {
            count++;
        }
        
        if (self.dataModel.tishi) {
            count++;
        }
        
        if (self.dataModel.goods) {
            count++;
        }
        
        if (self.dataModel.house) {
            count++;
        }
        
  
        
        return count;
    }
    
   
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.dataModel == nil) {
        return 0;
    } else {
        if (section == 0) {
            return 1;
        } else if (section == 1) {
            
            if (self.dataModel.advs) {
                return 1;
            }
            
            if (self.dataModel.articles) {
                return 1;
            }
            
            if (self.dataModel.huo) {
                return 1;
            }
            
            if (self.dataModel.goods) {
                return 1;
            }
            
            if (self.dataModel.tishi) {
                return 1;
            }
            
            if (self.dataModel.house) {
                return self.dataModel.house.count + 1;
            }
            
            return 1;
            
        }else if (section == 2) {
            
            
            NSInteger index = 0;
            
            if (self.dataModel.advs) {
                index++;
            }
            
            
            
            if (self.dataModel.articles) {
                
                if (index == 1) {
                    
                    return 1;
                }
                
                index++;

                
            }
            
            
            
            if (self.dataModel.huo) {
                
                if (index == 1) {
                    
                    return 1;
                }
                
                index++;
            }
            
            
            if (self.dataModel.tishi) {
                
                if (index == 1) {
                    
                    return 1;
                }
                
                index++;
                
            }
            
            
            if (self.dataModel.goods) {
                
                if (index == 1) {
                    
                    return 1;
                }
                
                index++;
            }
            
            if (self.dataModel.house) {
                
                if (index == 1) {
                    
                    return self.dataModel.house.count + 1;
                }
                
                index++;
            }
            
            return 0;
            
        }else if (section == 3) {
            
            NSInteger index = 0;
            
            if (self.dataModel.advs) {
                index++;
            }
            
            if (self.dataModel.articles) {
                
                index++;
                
            }
            
            if (self.dataModel.huo) {
                
                if (index == 2) {
                    
                    return 1;
                }
                
                index++;
            }
            
            
            if (self.dataModel.tishi) {
                
                if (index == 2) {
                    
                    return 1;
                }
                
                index++;
                
            }
            
            if (self.dataModel.goods) {
                
                if (index == 2) {
                    
                    return 1;
                }
                
                index++;
            }
            
            if (self.dataModel.house) {
                
                if (index == 2) {
                    
                    return self.dataModel.house.count + 1;
                }
                
                index++;
            }
            
            return 0;
            
            
        }else if (section == 4) {
            
            NSInteger index = 0;
            
            if (self.dataModel.advs) {
                index++;
            }
            
            if (self.dataModel.articles) {
                index++;
            }
            
            if (self.dataModel.huo) {
                index++;
            }
            
            
            if (self.dataModel.tishi) {
                
                if (index == 3) {
                    
                    return 1;
                }
                
                index++;
                
            }
            
            if (self.dataModel.goods) {
                
                if (index == 3) {
                    
                    return 1;
                }
                
                index++;
            }
            
            if (self.dataModel.house) {
                
                if (index == 3) {
                    
                    return self.dataModel.house.count + 1;
                }
                
                index++;
            }
            
            return 0;
            
        }else if (section == 5) {
            
            NSInteger index = 0;
            
            if (self.dataModel.advs) {
                index++;
            }
            
            if (self.dataModel.articles) {
                index++;
            }
            
            if (self.dataModel.huo) {
                index++;
            }
            
            
            if (self.dataModel.tishi) {
                
             index++;
                
            }
            
            if (self.dataModel.goods) {
                
                if (index == 4) {
                    
                    return 1;
                }
                
                index++;
            }
            
            if (self.dataModel.house) {
                
                if (index == 4) {
                    
                    return self.dataModel.house.count + 1;
                }
                
                index++;
            }
            
            return 0;
            
        }else{
            
            
            return self.dataModel.house.count + 1;
        }
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section == 0) {
        
        HomeProgramaCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeProgramaCell identifierStr]];
        
        cell.model = self.dataModel;
        
        return cell;
        
    } else if (indexPath.section == 1) {
        
        if (self.dataModel.advs) {
            
            HomeCycleImageCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCycleImageCell identifierStr]];
            
            cell.models = self.dataModel.advs;
            
            return cell;
            
        }
        
        if (self.dataModel.articles) {
            
            EveryDayTouTiaoCell *cell = [tableView dequeueReusableCellWithIdentifier:[EveryDayTouTiaoCell identifierStr]];
            
            cell.delegate = self;
            
            if ([self.dataModel.msgStatus isEqualToString:@"0"]) {
                cell.hideRedDian = YES;
            } else {
                cell.hideRedDian = NO;
                
            }
            
            cell.models = self.dataModel.articles;
            
            return cell;
            
        }
        
        if (self.dataModel.huo) {
            
            LifeSeviceSubCell *cell = [tableView dequeueReusableCellWithIdentifier:[LifeSeviceSubCell identifierStr]];
            
            
            cell.models = self.dataModel.huo;
            
            return cell;
  
        }
        
        
        if (self.dataModel.tishi) {
            
            HomeTiShiCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeTiShiCell identifierStr]];
            
            
            cell.models = self.dataModel.tishi;
            
            return cell;
            
        }
        
        
        if (self.dataModel.goods) {
            
            
            BuyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:[BuyRecommendCell identifierStr]];
            
            cell.model = self.dataModel.goods.firstObject;
            
            
            cell.delegate = self;

            return cell;
            
            
            
        }
        
        
        if (self.dataModel.house) {
            
            if (indexPath.row == 0) {
                
                HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
                
                return cell;
            } else {
                
                MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
                
                cell.model = self.dataModel.house[indexPath.row - 1];
                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
    }else if (indexPath.section == 2) {
        
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
            if (index == 1) {
                
                EveryDayTouTiaoCell *cell = [tableView dequeueReusableCellWithIdentifier:[EveryDayTouTiaoCell identifierStr]];
                
                cell.delegate = self;

                if ([self.dataModel.msgStatus isEqualToString:@"0"]) {
                    cell.hideRedDian = YES;
                } else {
                    cell.hideRedDian = NO;
                    
                }
                
                cell.models = self.dataModel.articles;

                
                return cell;
                
                
            }
            
            index++;
            
            
        }
        
        if (self.dataModel.huo) {
            
            if (index == 1) {
                
                LifeSeviceSubCell *cell = [tableView dequeueReusableCellWithIdentifier:[LifeSeviceSubCell identifierStr]];
                cell.models = self.dataModel.huo;

                return cell;
                
                
            }
            
            index++;
        }
        
        
        if (self.dataModel.tishi) {
            
            
            if (index == 1) {
                
                HomeTiShiCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeTiShiCell identifierStr]];
                cell.models = self.dataModel.tishi;
                
                return cell;
                
                
            }
            
            index++;
            
        }
        
        
        
        
        if (self.dataModel.goods) {
            
            if (index == 1) {
                
                BuyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:[BuyRecommendCell identifierStr]];
                
                cell.model = self.dataModel.goods.firstObject;
                cell.delegate = self;

                return cell;
                
            }
            
            index++;
        }
        
        if (self.dataModel.house) {
            
            if (indexPath.row == 0) {
                
                HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
                
                return cell;
                
            } else {
                
                MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
                
                cell.model = self.dataModel.house[indexPath.row - 1];

                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
    }else if (indexPath.section == 3) {
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            
            index++;
            
        }
        
        if (self.dataModel.huo) {
            
            if (index == 2) {
                
                LifeSeviceSubCell *cell = [tableView dequeueReusableCellWithIdentifier:[LifeSeviceSubCell identifierStr]];
                cell.models = self.dataModel.huo;

                return cell;
            }
            
            index++;
        }
        
        
        
        if (self.dataModel.tishi) {
            
            
            if (index == 2) {
                
                HomeTiShiCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeTiShiCell identifierStr]];
                cell.models = self.dataModel.tishi;
                
                return cell;
                
                
            }
            
            index++;
            
        }
        
        
        if (self.dataModel.goods) {
            
            if (index == 2) {
                
                BuyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:[BuyRecommendCell identifierStr]];
                cell.model = self.dataModel.goods.firstObject;
                cell.delegate = self;

                return cell;
                
            }
            
            index++;
        }
        
        if (self.dataModel.house) {
            if (indexPath.row == 0) {
                
                HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
                
                return cell;
                
            } else {
                
                MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
                
                cell.model = self.dataModel.house[indexPath.row - 1];

                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
    }else if (indexPath.section == 4) {
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            index++;
        }
        
        if (self.dataModel.huo) {
            index++;
        }
        
        
        if (self.dataModel.tishi) {
            
            
            if (index == 3) {
                
                HomeTiShiCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeTiShiCell identifierStr]];
                
                cell.models = self.dataModel.tishi;
                
                return cell;
                
                
            }
            
            index++;
            
        }
        
        
        if (self.dataModel.goods) {
            
            if (index == 3) {
                
                BuyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:[BuyRecommendCell identifierStr]];
                cell.model = self.dataModel.goods.firstObject;
                cell.delegate = self;

                return cell;
                
                
            }
            
            index++;
        }
        
        if (self.dataModel.house) {
            
            if (indexPath.row == 0) {
                
                HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
                
                return cell;
                
            } else {
                
                MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
                
                cell.model = self.dataModel.house[indexPath.row - 1];

                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
    }else if (indexPath.section == 5) {
        
        NSInteger index = 0;
        
        if (self.dataModel.advs) {
            index++;
        }
        
        if (self.dataModel.articles) {
            index++;
        }
        
        if (self.dataModel.huo) {
            index++;
        }
        
        
        if (self.dataModel.tishi) {
            
           
            index++;
            
        }
        
        
        if (self.dataModel.goods) {
            
            if (index == 4) {
                
                BuyRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:[BuyRecommendCell identifierStr]];
                cell.model = self.dataModel.goods.firstObject;
                cell.delegate = self;
                
                return cell;
                
                
            }
            
            index++;
        }
        
        if (self.dataModel.house) {
            
            if (indexPath.row == 0) {
                
                HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
                
                return cell;
                
            } else {
                
                MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
                
                cell.model = self.dataModel.house[indexPath.row - 1];
                
                
                return cell;
            }
            
            
        }
        
        BaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell identifierStr]];
        
        return cell;
        
        
    }else{
        
        
        if (indexPath.row == 0) {
            
            HotHouseHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:[HotHouseHeaderCell identifierStr]];
            
            return cell;
            
        } else {
            
            MyHouseInfoTopCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyHouseInfoTopCell identifierStr]];
            
            cell.model = self.dataModel.house[indexPath.row - 1];

            
            return cell;
        }
        
        
    }
    


}
   
    
- (void)recommendCellDidClickWith:(HomeSubModel *)subModel{
    
    
    GoodDetailController *tempVC = [[GoodDetailController alloc]init];
   
    OneGoodsModel *tempModel = [[OneGoodsModel alloc]init];
    
    tempModel.goods_id = subModel.goods_id;
    
    tempModel.goods_image_url = subModel.pic_cover_small;
    
    tempVC.goodsModel = tempModel;
    
    [self.navigationController pushViewController:tempVC animated:YES];
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.00001;

    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    return 0.00001;
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    

    
        return [[UIView alloc]init];;
        

    
}


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return [[UIView alloc]init];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if ([cell isKindOfClass:[MyHouseInfoTopCell class]]) {
        
        if (indexPath.row == 0) {
            
            
            
            
        } else {
            
            RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
            
            temp.houseId = self.dataModel.house[indexPath.row - 1].house_id;
            
            
            
            [self.navigationController pushViewController:temp animated:YES];
            
            
            
        }
        
    }
    
    
   
    


}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)openDengWithBtn:(UIButton *)btn{
    
    btn.selected = !btn.selected;
    
    Class captureDeviceclass = NSClassFromString(@"AVCaptureDevice");
    
    if (captureDeviceclass != nil) {
        AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        if ([device hasTorch]) {//判断是否有闪光灯
            
            //请求独占访问硬件设备
            [device lockForConfiguration:nil];
            
            if (btn.selected) {
                [device setTorchMode:AVCaptureTorchModeOn];//打开
            }else{
                
                [device setTorchMode:AVCaptureTorchModeOff];//关闭

            }
            
            
            [device unlockForConfiguration];//请求解除独占访问硬件设备
        }
    }
}



- (void)loadAreaData{
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"member/getArea" parameters:nil completionHandler:^(id responseObject, NSError *error) {
        
        
        if (!error) {
//        MYJSONSTR;
            
            myViewModel.areas = [ProvinceModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            
//            NSLog(@"%ld",myViewModel.areas.count);
            
        }
        
        
    }];
    
  
}





- (void)loadCategoryInfo{
    
//    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"house/getClassList" parameters:nil completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
//            NSLog(@"成功吧");
            
//            MYJSONSTR
            
            myViewModel.parameterModel = [HouseParameterModel mj_objectWithKeyValues:responseObject[@"data"]];
            
//            [weakSelf loadData];

        }
    }];
    
}


- (void)loadCommendGoods{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"uid"] = [UserViewModel shareUserViewModel].user.uid;
    
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/getCart" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
//            NSLog(@"成功吧");
//            MYJSONSTR
            
        myViewModel.tuiJianGoods = [MarketHSubModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"tuijian"]];
            
        }
    }];
    
}

@end
